<template>
    <div class="loginBox">
        <el-card class="box-card">
            <template #header>黑马面经运营后台</template>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm" >
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" type="password"></el-input>
                </el-form-item>
                <el-form class="button">
                        <el-button type="primary" @click="login">登 录</el-button>
                        <el-button @click="reset">重 置</el-button>
                </el-form>
            </el-form>

    </el-card>

    </div>
    
</template>

<script>
import { login } from "@/api/user.js";
import { saveToken } from "@/utils/token.js";
    export default {
        data() {
            return {
                ruleForm: {
                    username: 'admin',
                    password: 'admin',
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            
            login(){
                this.$refs.ruleForm.validate(flag=>{
                    if (flag) {
                        login(
                            this.ruleForm
                        ).then(res=>{
                            this.$message({
                            message: '恭喜你，登录成功',
                            type: 'success'
                            });
                            saveToken(res.data.token)
                            this.$router.push("/dashboard");
                        }).catch((err) => {
                        }) 
                    }
                    
                })
                
            },
            reset(){
                this.ruleForm.username=''
                this.ruleForm.password=''
            }
        },
    }
</script>

<style lang="scss" scoped>
    .el-card {
        width: 420px;
        ::v-deep .el-card__header{
        height: 80px;
        background: rgba(114,124,245,1);
        text-align: center;
        line-height: 40px;
        color: #fff;
        font-size: 18px;
        }
    }
    .loginBox{
        min-height: 100vh;
        background: url('../../assets/login-bg.svg') no-repeat center/cover;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .el-form {
        padding: 0 20px;
    }
    .button{
        text-align: center;
    }
</style>